package com.cabrida.livestar.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.main.SideBar.OnTouchingLetterChangedListener;
import com.cabrida.livestar.main.me.AttentionGvActivity;
import com.cabrida.livestar.model.FansVLogData;
import com.cabrida.livestar.star.StarActivity;
import com.cabrida.livestar.util.PinyinUtils;

/**
 * 粉丝端星部落
 * 
 * @author why
 */
public class FansVlogView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private ImageView iv_back_bg;
	private TextView tv_title;
	private TextView tv_num_attention;
	private LinearLayout ll_my_attention;
	private ListView listView;
	private SortAdapter sortadapter;
	private SideBar sidebar;
	private TextView dialog;
	private List<FansVLogData> data = new ArrayList<FansVLogData>();

	public FansVlogView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_fans_vlog, this);
		Init(view);
		
		Intent intent = new Intent();
		intent.setAction(MainActivity.ACTION_VLOG_LIST);
		mContext.sendBroadcast(intent);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		iv_back_bg = (ImageView) findViewById(R.id.iv_back_bg);
		tv_title = (TextView) findViewById(R.id.tv_title);
		iv_back_bg.setVisibility(View.INVISIBLE);
		tv_title.setText(mContext.getString(R.string.text_star_vlog));
		sidebar = (SideBar) view.findViewById(R.id.sidebar);
		listView = (ListView) view.findViewById(R.id.listview);
		dialog = (TextView) view.findViewById(R.id.dialog);
		tv_num_attention = (TextView) view.findViewById(R.id.tv_num_attention);
		ll_my_attention = (LinearLayout) view.findViewById(R.id.ll_my_attention);
		ll_my_attention.setOnClickListener(this);
		
		sidebar.setTextView(dialog);
		// 设置字母导航触摸监听
		sidebar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

			@Override
			public void onTouchingLetterChanged(String s) {
				// TODO Auto-generated method stub
				// 该字母首次出现的位置
				int position = sortadapter.getPositionForSelection(s.charAt(0));

				if (position != -1) {
					listView.setSelection(position);
				}
			}
		});
		
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FansVLogData data = (FansVLogData) arg0.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				intent.putExtra("suid", data.getS_udid().toString());
				intent.setClass(mContext, StarActivity.class);
				mContext.startActivity(intent);
			}
		});

		
	}
	
	/**
	 * 关注返回数据
	 */
	public void attentionData(String status,String s_udid,String number,String num){
		for (int i = 0; i < data.size(); i++) {
			if(s_udid.equals(data.get(i).getS_udid().toString())){
				data.get(i).setBooked(status);
				data.get(i).setS_booked(number);
			}
			
		}
		tv_num_attention.setText(num);
		sortadapter.refreshData(data);
		sortadapter.notifyDataSetChanged();
	}
	
	/**
	 * 初始化数据
	 */
	public void putData(ArrayList<FansVLogData> infoList,String subscribe){
		data = getDataByList(infoList);
//		data = getData(getResources().getStringArray(R.array.listpersons));
		// 数据在放在adapter之前需要排序
		Collections.sort(data, new PinyinComparator());
		sortadapter = new SortAdapter(mContext, data);
		listView.setAdapter(sortadapter);
		tv_num_attention.setText(subscribe);
	}
	
	private List<FansVLogData> getDataByList(ArrayList<FansVLogData> infoList) {
		List<FansVLogData> listarray = new ArrayList<FansVLogData>();
		for (int i = 0; i < infoList.size(); i++) {
			String pinyin = PinyinUtils.getPingYin(infoList.get(i).getS_name().toString());
			String Fpinyin = pinyin.substring(0, 1).toUpperCase();
			FansVLogData person = infoList.get(i);
			person.setPinYin(pinyin);
			// 正则表达式，判断首字母是否是英文字母
			if (Fpinyin.matches("[A-Z]")) {
				person.setFirstPinYin(Fpinyin);
			} else {
				person.setFirstPinYin("#");
			}

			listarray.add(person);
		}
		return listarray;

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_my_attention:
			Intent intent = new Intent();
			intent.setClass(mContext, AttentionGvActivity.class);
			mContext.startActivity(intent);
			break;
		default:
			break;
		}
	}

}
